/**
 * 
 */
package com.gopay.bis.psbc.ebank.impl;

import java.util.Map;

import com.gopay.bis.psbc.ebank.operation.PsbcB2cCertHelper;
import com.gopay.bis.psbc.ebank.operation.PsbcB2cQueryHelper;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.PsbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @PsbcB2cBankServiceImpl.java
 * @author Ryan
 * @2013-12-2 上午9:33:20 www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcB2cBankServiceImpl implements BankService {

    private PsbcB2cCertHelper psbcB2cCertHelper;
    private PsbcB2cQueryHelper psbcB2cQueryHelper;

    public Object sign(BaseMessage msg) throws GopayException {
        String singMsg = psbcB2cCertHelper.sign(msg);
        return singMsg;
    }

    public Object verify(BaseMessage msg) throws GopayException {
        boolean isVerifyed = (Boolean) psbcB2cCertHelper.verify(msg);
        return isVerifyed;
    }

    public Object query(Object obj) throws GopayException {
        PsbcB2cMessage  message = (PsbcB2cMessage) obj;
        // sign
        String signStr = psbcB2cCertHelper.sign(message);
        message.setSignMessage(signStr);
        // build query parameter
        Map<String, String> queryParam = psbcB2cQueryHelper.buildQueryParam(message);
        // query
        String queryResultStr = psbcB2cQueryHelper.query(queryParam);
        // parse query result of string to message entity
        PsbcB2cMessage queryResult = psbcB2cQueryHelper.parseMesage(queryResultStr);
        //verify while success
        if(PsbcConstants.PAY_RSP_CODE_SUCCESS.equals(queryResult.getRespCode())){
            queryResult.setOutStlmCd(BankConstants.BANK_LOGO_PSBC);
            boolean isVerify = (Boolean)psbcB2cCertHelper.verify(queryResult);
            if(isVerify){
                //psbcB2cQueryHelper.parsePlainMessage(queryResult);
            	return queryResult;
            }else{
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
            }
        }
        return queryResult;
    }

    /**
     * @param psbcB2cCertHelper
     *            the psbcB2cCertHelper to set
     */
    public void setPsbcB2cCertHelper(PsbcB2cCertHelper psbcB2cCertHelper) {
        this.psbcB2cCertHelper = psbcB2cCertHelper;
    }

    /**
     * @param psbcB2cQueryHelper
     *            the psbcB2cQueryHelper to set
     */
    public void setPsbcB2cQueryHelper(PsbcB2cQueryHelper psbcB2cQueryHelper) {
        this.psbcB2cQueryHelper = psbcB2cQueryHelper;
    }

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
